package com.yyw.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyw.bean.Homework;
import com.yyw.bean.User;
import com.yyw.bean.Working;
import com.yyw.dao.HomeWorkDao;
import com.yyw.util.MyContext;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "作业API")
@RestController
@RequestMapping("/user")
@Slf4j
public class HomeworkController {

    @Autowired
    private HomeWorkDao homeWorkDao;

    // 教师获取班级所有发布的作业信息
    @CrossOrigin
    @ApiOperation(value = "教师获取班级所有发布的作业信息", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "classid", value = "班级id", required = true) })
    @PostMapping("/getClassHomeworks")
    public Result getClassHomeworks(@RequestParam("classid") Long classid) {
        if (classid == null) {
            return ResultFactory.notice("数据不合法");
        }

        List<Homework> list = homeWorkDao.getHomeworksByClassId(classid);

        return ResultFactory.success(list);
    }

    // 教师修改发布的作业信息
    @CrossOrigin
    @ApiOperation(value = "教师修改发布的作业信息", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "作业id", required = true),
            @ApiImplicitParam(name = "title", value = "作业标题", required = true),
            @ApiImplicitParam(name = "intro", value = "作业描述", required = true) })
    @PostMapping("/updateHomework")
    public Result updateHomework(@RequestParam("id") Long id, @RequestParam("title") String title,
            @RequestParam("intro") String intro) {
        if (id == null || StringUtils.isEmpty(title) || StringUtils.isEmpty(intro)) {
            return ResultFactory.notice("数据不合法");
        }
        Homework homework = new Homework();
        homework.setId(id);
        homework.setTitle(title);
        homework.setIntro(intro);

        int i = homeWorkDao.updateHomework(homework);
        if (i > 0) {
            return ResultFactory.success("修改成功");
        } else {
            return ResultFactory.failure("修改失败");
        }
    }

    // 教师发布作业
    @CrossOrigin
    @ApiOperation(value = "教师发布作业", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "classid", value = "班级id", required = true),
            @ApiImplicitParam(name = "title", value = "作业标题", required = true),
            @ApiImplicitParam(name = "intro", value = "作业描述", required = true) })
    @PostMapping("/saveHomework")
    public Result saveHomework(@RequestParam("classid") Long classid, @RequestParam("title") String title,
            @RequestParam("intro") String intro) {
        if (classid == null || StringUtils.isEmpty(title) || StringUtils.isEmpty(intro)) {
            return ResultFactory.notice("数据不合法");
        }
        Homework homework = new Homework();
        homework.setClassid(classid);
        homework.setTitle(title);
        homework.setIntro(intro);

        int i = homeWorkDao.saveHomeWork(homework);
        if (i > 0) {
            return ResultFactory.success("添加成功");
        } else {
            return ResultFactory.failure("添加失败");
        }
    }

    // 教师删除曾经发布的作业
    @CrossOrigin
    @ApiOperation(value = "教师删除曾经发布的作业", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "作业id", required = true) })
    @PostMapping("/deleteHomework")
    public Result deleteHomework(@RequestParam("id") Long id) {
        if (id == null) {
            return ResultFactory.notice("数据不合法");
        }
        int i = homeWorkDao.deleteHomework(id);
        if (i > 0) {
            return ResultFactory.success("删除成功");
        } else {
            return ResultFactory.failure("删除失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "给作业打分", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "workingid", value = "已提交作业的id", required = true),
            @ApiImplicitParam(name = "score", value = "分数", required = true) })
    @PostMapping("/dealWorking")
    public Result dealWorking(@RequestParam("workingid") Long workingid, @RequestParam("score") Integer score) {
        if (workingid == null || score == null) {
            return ResultFactory.notice("数据不合法");
        }
        Working working = new Working();
        working.setId(workingid);
        working.setScore(score);
        working.setState(2);
        int i = homeWorkDao.updateWorking(working);
        if (i > 0) {
            return ResultFactory.success("修改成功");
        } else {
            return ResultFactory.failure("修改失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "修改已提交的作业内容", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "workingid", value = "已提交作业的id", required = true),
            @ApiImplicitParam(name = "content", value = "内容", required = true) })
    @PostMapping("/updateWorking")
    public Result updateWorking(@RequestParam(value = "workingid") Long workingid,
            @RequestParam(value = "content") String content) {
        if (workingid == null || StringUtils.isEmpty(content)) {
            return ResultFactory.notice("数据不合法");
        }
        Working working = new Working();
        working.setId(workingid);
        working.setContent(content);
        int i = homeWorkDao.updateWorking(working);

        if (i > 0) {
            return ResultFactory.success("修改成功");
        } else {
            return ResultFactory.failure("修改失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "学生提交作业", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "homeworkid", value = "作业id", required = true),
            @ApiImplicitParam(name = "content", value = "完成的作业内容", required = true) })
    @PostMapping("/submitWorking")
    public Result submitWorking(@RequestParam("homeworkid") Long homeworkid, @RequestParam("content") String content) {
        if (homeworkid == null || StringUtils.isEmpty(content)) {
            return ResultFactory.notice("数据不合法");
        }

        User user = MyContext.getSessionUser();
        if (user.getClassid() == null) {
            return ResultFactory.notice("未加入班级");
        }
        Working working = new Working();
        working.setUserid(user.getId());
        working.setClassid(user.getClassid());
        working.setHomeworkid(homeworkid);
        working.setContent(content);
        int i = homeWorkDao.saveWorking(working);
        if (i > 0) {
            return ResultFactory.success("提交成功");
        } else {
            return ResultFactory.failure("提交失败");
        }

    }

    @CrossOrigin
    @ApiOperation(value = "根据id获取已提交作业的详细信息", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "已提交作业的id", required = true) })
    @GetMapping("/getWorking")
    public Result getWorking(@RequestParam("id") Long id) {

        if (id == null) {
            return ResultFactory.notice("数据不合法");
        }
        Working working = homeWorkDao.getWorkingById(id);
        return ResultFactory.success(working);
    }

    @CrossOrigin
    @ApiOperation(value = "学生获取自己在当前班级提交的作业情况，分页", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getgetWorkings")
    public Result getgetWorkings(@RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum) {
        if (pageNo < 1 || pageNum < 1) {
            return ResultFactory.notice("数据不合法");
        }
        User user = MyContext.getSessionUser();
        if (user.getClassid() == null) {
            return ResultFactory.notice("未加入班级");
        }

        PageHelper.startPage(pageNo, pageNum);
        List<Homework> workings = homeWorkDao.getWorkings(user.getId(), user.getClassid());

        PageInfo<Homework> pageInfo = new PageInfo<>(workings);
        return ResultFactory.success(pageInfo);
    }

    @CrossOrigin
    @ApiOperation(value = "教师获取指定作业提交的作业情况，分页", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "homeworkid", value = "作业id"),
            @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getWorkingsByHomeWorkId")
    public Result getWorkingsByHomeWorkId(@RequestParam(value = "homeworkid") Long homeworkid,
            @RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum) {
        if (pageNo < 1 || pageNum < 1) {
            return ResultFactory.notice("数据不合法");
        }
        if (homeworkid == null) {
            return ResultFactory.notice("数据不合法");
        }

        PageHelper.startPage(pageNo, pageNum);
        List<Working> workings = homeWorkDao.getWorkingsByHomeWorkId(homeworkid);

        PageInfo<Working> pageInfo = new PageInfo<>(workings);
        return ResultFactory.success(pageInfo);
    }
}
